#include <lcd/qc1602a.h>


int main(void)
{
  init();

  Serial.begin(115200);

  lcd::qc1602a screen(12, 11, 10, 9, 2, 3, 4, 5);

  int cursor = 0;
  int screen_lines = screen.get_nb_lines();
  int screen_columns = screen.get_nb_columns();
  int max_cursor = screen_lines * screen_columns;

  char buffer[2] = { '\0', '\0' };

  while(true)
  {
    int val = Serial.read();
    if (val != -1)
    {
      int current_line = cursor/screen_columns;
      int current_column = cursor % screen_columns;
      switch (char(val))
      {
        case '\n':
        {
          int last_cursor = (current_line+1) * screen_columns;
          *buffer = ' ';

          while (current_column != screen_columns)
          {
            screen.print(current_line,
                         cursor % screen_columns,
                         buffer);
            ++cursor;
            ++current_column;
          }
          break;
        }
        default:
        {
          *buffer = char(val);
          screen.print(current_line,
                       cursor % screen_columns,
                       buffer);
          ++cursor;
        }
      }
      cursor %= max_cursor;
    }

    delay(50);
  }
}
